<template>
  <div class="menu">
    <van-nav-bar title="菜单" :fixed="true"></van-nav-bar>
    <div class="classicMenu">
      <div class="cm-left">
        <van-sidebar class="van-sidebar" v-model="activeKey">
          <van-sidebar-item
            v-for="(item, index) in menuType"
            :key="index"
            :title="item.typeDesc"
            :class="{ active: activeKey === index }"
            @click="getcommodity(item.type)"
          />
        </van-sidebar>
      </div>
      <div class="cm-right">
        <div
          class="cm-right-item"
          v-for="(item, index) in commodity"
          :key="index"
          @click="toDetail(item.pid)"
        >
          <div class="l">
            <img :src="item.largeImg" alt="" />
          </div>
          <div class="r">
            <p class="name">
              {{ item.name
              }}<span class="hot" v-if="item.isHot == 1">热卖</span>
            </p>
            <p class="enname">{{ item.enname }}</p>
            <p class="desc">{{ item.desc }}</p>
            <p class="price">&yen;{{ item.price }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Menu",
  data() {
    return {
      active: 0,
      activeKey: 0,
      menuType: [],
      commodity: [],
    };
  },
  created() {
    this.getcommodityType();
  },
  methods: {
    getcommodityType() {
      this.axios({
        method: "get",
        url: "/type",
        params: {},
      })
        .then((result) => {
          let res = result.data.result;
          res.unshift({ type: "isHot", typeDesc: "热卖推荐" });
          this.menuType = res;
          let type = res[this.activeKey].type;
          this.getcommodity(type);
        })
        .catch((err) => {

        });
    },
    getcommodity(type) {
      let key = "";
      let value = "";
      if (type == "isHot") {
        key = "isHot";
        value = 1;
      } else {
        key = "type";
        value = type;
      }
      this.axios({
        method: "get",
        url: "/typeProducts",
        params: {
          key,
          value,
        },
      }).then((result) => {
        this.commodity = result.data.result;
      });
    },
    toDetail(pid) {

      this.$router.push({ name: "Detail", params: { pid } });
    },
  },
};
</script>

<style lang="less" scoped>
.menu {
  padding-top: 76px;
  .van-nav-bar--fixed{
    padding-top: 30px;
    background-color: #0022ab;
  }
  /deep/.van-nav-bar__title{
    color: #fff;
  }
  .title {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    font-weight: 600;
    position: fixed;
    top: 0;
    background-color: #fff;
  }
  .van-tabs {
    margin-top: 40px;
  }
  .classicMenu {
    position: fixed;
    top: 76px;
    bottom: 50px;
    left: 0;
    right: 0;
    display: flex;
    padding: 10px 0;
    .cm-left {
      width: 80px;
      height: 100%;
      .van-sidebar {
        width: 100%;
        position: relative;
        .van-sidebar-item {
          background-color: #eee;
          height: 40px;
          display: flex;
          align-items: center;
          border-radius: 0 30px 30px 0;
          &.active {
            background-color: #ccc;
          }
        }
        .van-sidebar-item--select::before {
          position: absolute;
          left: -8px;
          width: 15px;
          height: 15px;
          border-radius: 50%;
          background-color: #0022ab;
        }
      }
    }
    .cm-right {
      width: calc(100% - 100px);
      height: 100%;
      background-color: #fff;
      margin: 0 10px;
      border-radius: 10px;
      padding: 10px;
      overflow-y: auto;
      .cm-right-type {
        width: 100%;
        .typeDesc {
          font-size: 16px;
          font-weight: 600;
          color: #0022ab;
        }
      }
      .cm-right-item {
        width: 100%;
        display: flex;
        margin: 20px 0;
        .l {
          margin-right: 10px;
          img {
            width: 80px;
            height: 80px;
            border-radius: 50%;
          }
        }
        .r {
          p {
            width: 160px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .name {
            font-size: 15px;
            color: #555;
            .hot {
              margin-left: 10px;
              background-color: red;
              font-size: 10px;
              color: #fff;
              padding: 2px 8px;
              border-radius: 10px 0 10px 0;
            }
          }
          .enname {
            color: #999;
          }
          .desc {
            color: #777;
            margin: 6px 0;
          }
          .price {
            font-size: 15px;
            font-weight: 600;
            color: #0022ab;
          }
        }
      }
    }
  }
}
</style>